<?php
class UserAction extends BaseAction{
	function index_simple(){
		//echo '默认方法';	
		$user = new UserModel();
		$list = $user->select();
		$this->assign('title','用户列表');
		$this->assign('ulist',$list);	
		
		//第一种实例化模型	
		$user = new Model('User'); //参数为除去表前缀的表名
		
		//第二种实例化模型
		$user = M('User');
		
		//第三种实例化模型
		$user = new UserModel();
		
		//第四种实例化模型
		$user = D('User');
		M('User', 'CommonModel'); //<=>  new CommonModel('User');  用于重复性操作
		D('User', '17Joys'); //<=>17Joys/Model,17Joys下的Model,第二个参数为项目名,在前台的时候使用
		dump($user);
		
		$this->display();
	}
	
	function index_all(){
		import('ORG.Util.Page');
		$user = new UserModel();
		$keyword = $_POST['keyword'];
		$ftype = $_POST['ftype'];
		if(!empty($keyword) && !empty($ftype)){
			$where[$ftype] = array('like','%'.$keyword.'%');
			$_SESSION['keyword'] = $where;
		}else{
			if(empty($keyword) &&!empty($ftype)){
				unset($_SESSION['keyword']);
			}else if(!empty($_SESSION['keyword'])){
				$where = $_SESSION['keyword'];
			}
		}
		
		$count = $user->where($where)->count();
		$page=new Page($count,C('PAGESIZE'));
		$show = $page->show();
		$this->assign('show',$show);
		$list = $user->order('id')->where($where)
		->limit($page-firstRow.','.$page->listRows)->select();
		$this-assign('ulist',$list);
		$this->display();
	}
	
function index(){
		//创建模型
		$user = new UserModel();
		//接收查询的表单数据
		$keywords = $_POST['keywords'];
		$ftype = $_POST['ftype'];
		//判断表单是否提交数据
		//$where = '';
		if(!empty($keywords) && !empty($ftype)){
			//$where = $ftype." like '%".$keywords."%'";
			//数组方式
			//$where[$ftype] = $keywords;
			//$where[$ftype] = array('like', "%".$keywords."%");
			//$where[$ftype] = array('gt', $keywords);
			//$where[$ftype] = array(array('gt', 1),array('lt',4),'or');
			$where['username'] = array('like', '%z%');
			$where['id'] = array('gt', 2);
			$where['_logic'] = 'or';
			$map['_complex'] = $where;
			$map['active'] = 1;
			echo $map;
		}
		
		$list = $user->where($where)->select();
		
		//$data = $user->find(); //取一条数据
		//$data = $user->getField('id,username,name,active');
		//$list = $user->where($where)->findAll();
		//$list = $user->where($map)->findAll();
		//dump($list);
		
		$this->assign('ulist', $list);
		//dump($list);
		/*
		//统计
		$usrCount['username'] = array('like', '%z%');
		$userCount = $user->where($usrCount)->count();
		dump($userCount);
		//最小值
		$minId = $user->where('id>=3')->min('id');
		dump($minId);
		
		//平均值
		$avgId = $user->avg('id');
		dump($avgId);
		
		//总和
		$sumId = $user->sum('id');
		dump($sumId);
		
		//不行,查询有异常
		//$User = M("User");
		//$User->where('id>1')->order('id desc')->getN(2);
		//dump($User);
		//$tmp = $user->where('id>1')->order('id desc')->last();
		//dump($tmp);
		
		//query方法
		$tmp = $user->query("select * from joys_user");
		//dump($tmp);
		
		//execute方法
		//$tmp2 = $user->execute("update joys_user set active=1");
		//dump($tmp2);
		
		
		//$data = $user->getById(1);
		//$data = $user->getByName('zjmhappy');
		//$data = $user->getByUsername('tomcat');
		//此方法不行
		//$data = $user->where('id>1')->order('id desc')->select();//->top5();
		//分页
		$data = $user->page('1,2')->select();
		$data = $user->group('active')->select();
		$data = $user->group('active')->having('active=1')->select();
		//$user->lock(true)->select();
		dump($data);
		*/
		$this->display();
	}
	
	/**
	 * 
	 * 创建用户页面
	 */
	function add(){
		//$role = new Model('Role');
		//$list = $role->select();
		//$this-assign('rlist',$list);		
		$this->display();
	}
	
	/**
	 * 
	 * 插入用户数据
	 */
	function insert(){
		//创建用户模型
		$user = new UserModel();  //D('User');
		$data=$user->create();  //表单验证和自动填充都是通过create()完成
		if($data){
			//表单验证成功,进行数据插入
			//dump($data);  //返回bool(false)是表单数据不符合
			//dump($user);
			//$user->add($data);
			
			if(false !== $user->add()){//有返回值
				$userid = $user->getLastInsID(); //获取最新数据编号,只适用于自动增长列
				echo '创建成功，用户编号:'.$userid;
			}else{
				echo '创建失败，'.$user->getDbError();
			}
		}else{
			//验证失败
			echo $user->getError();
		}
		
		
	}
	
	/**
	 * 
	 * 用户编辑页面
	 */
	function edit(){
		$id = $_GET['id'];
		if(!empty($id)){
			$user = new UserModel();
			$data = $user->getById($id);
			$this->assign('user',$data);
		} else{
			echo '请选择要编辑的用户';
			return;
		}
		$this->display();
		/*
		
		$is=$_GET['id'];
		if(!empty($id)){
			$user = new UserModel();
			$data = $user->field('id,username,name,password,email,active,role_id')
			->join('joys_role_user on id=user_id')->getById($id);
			$this-assign('udata',$data);
			$role = new Model('Role');
			$list = $role->select();
			$this->assign('rlist',$list);
			
		}
		*/
	}
	
	/**
	 * 
	 * 更新用户数据
	 */
	function update(){
		$user = new UserModel();
		if($data = $user->create()){
			if(!empty($data['id'])){
				if(false !== $user->save()){
					echo '修改用户资料成功!';
				}else{
					echo '修改用户资料失败!'.$user->getDbError();;
				}
			}else{
				echo '请选择要修改的用户!';
			}
			
		} else{
			echo '修改用户资料失败,'.$user->getDbError();
		}
	}
	
	/**
	 * 
	 * 删除用户信息
	 */
	function delete(){
		$id = $_POST['id'];
		if(!empty($id)){
			$user = new UserModel();
			$userId = implode(',', $id);
			//$where['id'] = array('eq',$id);
			$where['id'] = array('in',$userId);
			$data = $user->where($where)->delete();
			dump($data);
			if(true == $data){
				
				echo '删除用户信息成功！';
			}else{
				//echo '删除用户信息失败！';
				$this->error('删除用户信息失败！');
			}
		}else{
			//echo '请选择要删除的用户信息！';
			$this->error('请选择要删除的用户信息！');
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}

?>